

package AddressBook.Model;


import AddressBook.Model.Exception.NoSuchKeyFoundHashMapException;
import AddressBook.Model.Exception.KeyAlreadyUsedHashMapException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * The Class HardwareStoreManager allows to manage the different contacts family created
 */
public class GroupManager {

    /**
     * This field keeps the reference of contact family created, each contact family will be assigned to a key to easily retrieve them
     */
    private HashMap<String, Group> groupHashMap;

    /**
     * This Constructor create and initialize
     */
    public GroupManager() {
        groupHashMap = new HashMap<String, Group>();
    }

    /**
     * This method allows to split the specified contact list into different product family
     *
     * @param contactListFile The contactListFile to split
     * @throws IllegalArgumentException       If the contactlist is empty
     * @throws KeyAlreadyUsedHashMapException If two contacts have the same code
     */
    public void splitContactPerGroup(ArrayList<Contact> contactListFile) throws IllegalArgumentException, KeyAlreadyUsedHashMapException {
        groupHashMap = new HashMap<String, Group>();
        if (contactListFile.size() > 0) {
            for (Contact i : contactListFile) addContact(i);
        } else throw new IllegalArgumentException("Empty contactListFile");
    }

    /**
     * This method returns an array containing all the contact families created
     *
     * @return An array of all the contact families
     */
    public final Group[] getGroupArray() throws NullPointerException {
        Group[] p = new Group[groupHashMap.size()];
        return groupHashMap.values().toArray(p);
    }

    /**
     * This method returns the contact family associated to the familyKey
     *
     * @param familyKey The name of the family
     * @return The associated contact family
     * @throws AddressBook.Model.Exception.NoSuchKeyFoundHashMapException
     *          If no family found
     */
    public final Group getGroup(String familyKey) throws NoSuchKeyFoundHashMapException {
        if (groupHashMap.containsKey(familyKey)) return groupHashMap.get(familyKey);
        throw new NoSuchKeyFoundHashMapException("No family found");
    }


    /**
     * This method returns the contact associated to the phone
     *
     * @param phone The phone of the item
     * @return The associated item
     * @throws AddressBook.Model.Exception.NoSuchKeyFoundHashMapException
     *          If no item found
     */
    public Contact getItem(String phone) throws NoSuchKeyFoundHashMapException {

        for (Map.Entry<String, Group> entry : groupHashMap.entrySet()) {
            Group p = entry.getValue();
            if (p.containsItem(phone)) return p.getItem(phone);
        }
        throw new NoSuchKeyFoundHashMapException("Contact not found");

    }

    /**
     * This method returns an arrayList of all contacts created
     *
     * @return An arrayList containing all contacts created
     */
    public final ArrayList<Contact> getContactList() {
        ArrayList<Contact> contactList = new ArrayList<Contact>();
        for (Map.Entry<String, Group> entry : groupHashMap.entrySet()) {
            Collections.addAll(contactList, entry.getValue().getItemArray());
        }
        return contactList;
    }

    /**
     * This method allows to add an contact
     *
     * @param i The contact to add
     * @throws KeyAlreadyUsedHashMapException If the contact has been already added
     */
    public void addContact(Contact i) throws KeyAlreadyUsedHashMapException {
        if (!groupHashMap.containsKey(i.getGroupName()))
            groupHashMap.put(i.getGroupName(), new Group(i.getGroupName()));

        groupHashMap.get(i.getGroupName()).addContact(i);
    }

    /**
     * This method allows to remove an contact
     *
     * @param i The contact to remove
     * @throws NoSuchKeyFoundHashMapException If the contact doesn't exist
     */
    public void removeContact(Contact i) throws NoSuchKeyFoundHashMapException {
        if (groupHashMap.containsKey(i.getGroupName())) {
            groupHashMap.get(i.getGroupName()).removeContact(i);
        } else throw new NoSuchKeyFoundHashMapException();
    }
}
